﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Emulator.GameBoyConfig;

namespace Emulator.GameBoy.GPU
{
    public class GameBoyTile
    {
        public int WidthPerTile { get; set; }
        public int HeightPerTile { get; set; }
        public int TileNumber { get; set; }
        public byte[] TileColorData { get; set; }

        public GameBoyTile(int aTileNumber, byte[] tileColorData)
        {
            TileNumber = aTileNumber;
            TileColorData = tileColorData;
            WidthPerTile= GameBoyGPUConfig.TileWidth;
            HeightPerTile = tileColorData.Length/WidthPerTile;
        }

        public void Output(ref byte[] aOutputContainer, int aFrameWidth)
        { 
            int mTileCountPerLine =(aFrameWidth / WidthPerTile);
            //确定当前tile在整个输出帧中是第几行第几个tile
            int mRow = TileNumber / mTileCountPerLine;
            int mCol = TileNumber % mTileCountPerLine;
            int mTileRow, mTileCol,mPixIndexInFrame;
            for (int i = 0; i < TileColorData.Length; i++)
            { 
                //确定tile中的像素点在tile中第几行第几列
                mTileRow = i / WidthPerTile;
                mTileCol = i % WidthPerTile;

                mPixIndexInFrame = (mRow * HeightPerTile + mTileRow) * aFrameWidth + (mCol * WidthPerTile + mTileCol);
                aOutputContainer[mPixIndexInFrame] = TileColorData[i];
            }
        }
    }
}
